package group.igfd.manager.biz.controller;

import group.igfd.manager.biz.entity.DrawbackRecordEntity;
import group.igfd.manager.biz.service.DrawbackRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;


/**
 * 退税申请记录表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-12-12 18:55:43
 */
@RestController
@RequestMapping("biz/drawbackRecord")
public class DrawbackRecordController {
    @Autowired
    private DrawbackRecordService drawbackRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:drawbackRecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = drawbackRecordService.queryData(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/export")
    @RequiresPermissions("biz:drawbackRecord:list")
    public void export(HttpServletResponse httpServletResponse, @RequestParam Map<String, Object> params) {
        drawbackRecordService.export(httpServletResponse, params);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:drawbackRecord:info")
    public R info(@PathVariable("id") Integer id) {
        Map<String, Object> map = drawbackRecordService.getInfo(id);
        if (map.get("isSuccess").toString() == "false") {
            return R.error("获取信息失败");
        } else {
            return R.ok().put("drawbackRecord", map);
        }
    }

    @RequestMapping("/pass")
    @RequiresPermissions("biz:drawbackRecord:info")
    public R pass(@RequestBody Map<String, String> map) {
        Integer currenttId = Integer.parseInt(map.get("currenttId"));
        Integer states = Integer.parseInt(map.get("states"));
        Integer taxPrice = Integer.parseInt(map.get("taxPrice"));
        Integer taxPoint = Integer.parseInt(map.get("taxPoint"));
        String reason = map.get("reason");
        if (states == 1) {
            if (taxPrice == null || taxPoint == null || taxPrice == 0 || taxPoint == 0) {
                return R.error("退税金额或税点为空");
            }
        }
        return drawbackRecordService.PassorNoPass(currenttId, states, taxPrice, taxPoint, reason);
    }

    @RequestMapping("/updateStatus")
    @RequiresPermissions("biz:drawbackRecord:info")
    public R updateStatus(@RequestBody Map<String, String> map) {
        return drawbackRecordService.updateStatus(Integer.parseInt(map.get("id")), Integer.parseInt(map.get("status")));
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:drawbackRecord:save")
    public R save(@RequestBody DrawbackRecordEntity drawbackRecord) {
        drawbackRecordService.insert(drawbackRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:drawbackRecord:update")
    public R update(@RequestBody DrawbackRecordEntity drawbackRecord) {
        drawbackRecordService.updateById(drawbackRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:drawbackRecord:delete")
    public R delete(@RequestBody Integer[] ids) {
        drawbackRecordService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
